﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using Register.DBUtility;
using System.Data.SqlClient;
using System.Data;
using Register.Model;

namespace Register.DAL
{
    /// <summary>
    /// FileName: AdminInfoDAL.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangYan
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/6/26 9:47:00
    /// </summary>
   public class AdminInfoDAL : IAdminInfoDAL
    {

       private string tableName = "System_adminInfo";

       public int AddAdminInfo(Model.AdminInfo model)
       {

           StringBuilder strSql = new StringBuilder();
           strSql.Append("insert into System_adminInfo(");
           strSql.Append("Administrator_ID,Administrator_Name,Administrator_Password,Administrator_Hosptial,Administrator_AddTime,Administrator_Recoder,Administrator_LastRegTime)");
           strSql.Append(" values (");
           strSql.Append("@Administrator_ID,@Administrator_Name,@Administrator_Password,@Administrator_Hosptial,@Administrator_AddTime,@Administrator_Recoder,@Administrator_LastRegTime)");
           SqlParameter[] parameters = {
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14),
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar,20),
					new SqlParameter("@Administrator_Password", SqlDbType.Char,32),
					new SqlParameter("@Administrator_Hosptial", SqlDbType.Char,5),
					new SqlParameter("@Administrator_AddTime", SqlDbType.DateTime),
					new SqlParameter("@Administrator_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@Administrator_LastRegTime", SqlDbType.DateTime)};
           parameters[0].Value = model.Administrator_ID;
           parameters[1].Value = model.Administrator_Name;
           parameters[2].Value = model.Administrator_Password;
           parameters[3].Value = model.Administrator_Hosptial;
           parameters[4].Value = model.Administrator_AddTime;
           parameters[5].Value = model.Administrator_Recoder;
           parameters[6].Value = model.Administrator_LastRegTime;

           return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

       }

        public int DeleteAdminInfo(string adminID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_adminInfo ");
            strSql.Append(" where Administrator_ID=@Administrator_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14)};
            parameters[0].Value = adminID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        public int UpdateAdminInfo(Model.AdminInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_adminInfo set ");
            strSql.Append("Administrator_Name=@Administrator_Name,");
            strSql.Append("Administrator_Password=@Administrator_Password,");
            strSql.Append("Administrator_Hosptial=@Administrator_Hosptial ");
            strSql.Append(" where Administrator_ID=@Administrator_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar,20),
					new SqlParameter("@Administrator_Password", SqlDbType.Char,32),
					new SqlParameter("@Administrator_Hosptial", SqlDbType.Char,5),
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14)};
            parameters[0].Value = model.Administrator_Name;
            parameters[1].Value = model.Administrator_Password;
            parameters[2].Value = model.Administrator_Hosptial;
            parameters[3].Value = model.Administrator_ID;
            return DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        }

        public List<Model.AdminInfo> GetPagedAdminInfo(int size, int pageIndex, string where)
        {
            return DataSetToList(DbHelperSQL.getPagedData(" * "," Administrator_AddTime ",where,tableName,OrderBy.DESC,pageIndex,size));
        }


        public bool Exists(string Administrator_Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_adminInfo");
            strSql.Append(" where Administrator_Name=@Administrator_Name ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar)};
            parameters[0].Value = Administrator_Name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        public Model.AdminInfo GetAdminInfo(string adminID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Administrator_ID,Administrator_Name,Administrator_Password,Administrator_Hosptial,Administrator_AddTime,Administrator_Recoder,Administrator_LastRegTime from System_adminInfo ");
            strSql.Append(" where Administrator_ID=@Administrator_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_ID", SqlDbType.Char,14)};
            parameters[0].Value = adminID;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(), parameters)).First();
        }


        private List<Model.AdminInfo> DataSetToList(DataSet ds)
        {
            List<Model.AdminInfo> list = new List<Model.AdminInfo>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                AdminInfo admin = new AdminInfo();
                admin.Administrator_ID = (string)dr["Administrator_ID"];
                admin.Administrator_AddTime = (DateTime)dr["Administrator_AddTime"];
                admin.Administrator_Hosptial = (String)dr["Administrator_Hosptial"];
                admin.Administrator_LastRegTime = (DateTime)dr["Administrator_LastRegTime"];
                admin.Administrator_Name = (String)dr["Administrator_Name"];
                admin.Administrator_Password = (String)dr["Administrator_Password"];
                admin.Administrator_Recoder = (String)dr["Administrator_Recoder"];
                list.Add(admin);
            }
            return list;
        }

        public int GetAdminInfoCount(string where)
        {
            int count = 0;
            StringBuilder str = new StringBuilder();
            str.Append(" select count(*) from ");
            str.Append(tableName);
            if (where != null)
            {
                str.Append(" where ");
                str.Append(where);
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            else
            {
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            return count;
        }
    }
}
